from django.contrib import admin
from inventory.models import (
    Warehouse,
    Location,
    Inventory,
    InventoryTransaction,
    InventoryCheck,
    InventoryCheckDetail
)


@admin.register(Warehouse)
class WarehouseAdmin(admin.ModelAdmin):
    list_display = ('warehouse_code', 'warehouse_name', 'status', 'manager_id', 'is_primary', 'create_time')
    search_fields = ('warehouse_code', 'warehouse_name', 'contact_person')
    list_filter = ('status', 'is_primary')
    ordering = ('warehouse_code',)


@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
    list_display = ('warehouse', 'location_code', 'location_name', 'status', 'zone', 'row', 'column', 'level')
    search_fields = ('location_code', 'location_name')
    list_filter = ('warehouse', 'status', 'location_type')
    ordering = ('warehouse', 'location_code')


@admin.register(Inventory)
class InventoryAdmin(admin.ModelAdmin):
    list_display = ('product', 'warehouse', 'location', 'quantity', 'available_quantity', 'reserved_quantity', 'unit_cost', 'lot_number')
    search_fields = ('product__name', 'product__sku', 'lot_number', 'serial_number')
    list_filter = ('warehouse', 'location')
    ordering = ('product', 'warehouse', 'location')


@admin.register(InventoryTransaction)
class InventoryTransactionAdmin(admin.ModelAdmin):
    list_display = ('transaction_type', 'product', 'from_warehouse', 'to_warehouse', 'quantity', 'create_time', 'reference_document')
    search_fields = ('product__name', 'product__sku', 'reference_document')
    list_filter = ('transaction_type', 'from_warehouse', 'to_warehouse')
    ordering = ('-create_time',)


@admin.register(InventoryCheck)
class InventoryCheckAdmin(admin.ModelAdmin):
    list_display = ('check_code', 'warehouse', 'status', 'check_date', 'checker_id', 'approver_id', 'total_variance_quantity')
    search_fields = ('check_code',)
    list_filter = ('status', 'warehouse')
    ordering = ('-check_date',)


@admin.register(InventoryCheckDetail)
class InventoryCheckDetailAdmin(admin.ModelAdmin):
    list_display = ('inventory_check', 'product', 'location', 'system_quantity', 'actual_quantity', 'variance_quantity')
    search_fields = ('inventory_check__check_code', 'product__name', 'product__sku')
    list_filter = ('inventory_check__warehouse', 'location')
    ordering = ('inventory_check', 'product')